package cn.kgc.springmvc.controller;

import cn.kgc.springmvc.util.ResponseResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 课工场
 * @date 2024/2/28
 * @description  cookie和session中值进行操作
*    @SessionAttribute
*    @CookieValue
 */
@Controller
@RequestMapping("cs")
public class CSController {


    @RequestMapping("m1")
    @ResponseBody
    public ResponseResult  method1(HttpSession session, HttpServletResponse response){

        String id = session.getId();
        System.out.println("id = " + id);


        //  创建cookie对象
        Cookie cookie1 = new Cookie("ck1", "hello");
        Cookie cookie2 = new Cookie("ck2", "word");

        // 将cookie对象绑定在response对象上
        response.addCookie(cookie1);
        response.addCookie(cookie2);

        session.setAttribute("session","sessionValue:"+id);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMessage("请求成功");
        return  responseResult;
    }

    @RequestMapping("m2")
    @ResponseBody
    public ResponseResult  method2(HttpSession session){

        Object info = session.getAttribute("session");
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMessage("请求成功");
        responseResult.setData(info);
        return  responseResult;
    }


    //  使用springmvc框架中提供的注解 实现session域中值的获取  @SessionAttribute
    @RequestMapping("m3")
    @ResponseBody
    public ResponseResult  method3(@SessionAttribute("session") String sessionValue){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMessage("请求成功");
        responseResult.setData(sessionValue);
        return  responseResult;
    }

    @RequestMapping("m4")
    @ResponseBody
    public ResponseResult  method4(HttpServletRequest request){

        ResponseResult responseResult = new ResponseResult();
        Cookie[] cookies = request.getCookies();
        String info = "";

        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if ("ck1".equals(name)||"ck2".equals(name)){
                System.out.println(name+":"+cookie.getValue());
                info+= name+":"+cookie.getValue();
            }
        }
        responseResult.setMessage("请求成功");
        responseResult.setData(info);
        return  responseResult;
    }



    //  springmvc 框架中可以使用@CookieValue注解实现cookie中参数的快速获取
    @RequestMapping("m5")
    @ResponseBody
    public ResponseResult  method5(@CookieValue("ck1") String ck1,@CookieValue("ck2") String ck2){

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(0);
        responseResult.setMessage("请求成功");
        responseResult.setData(ck1+" "+ck2);
        return  responseResult;

    }

}
